import { prisma } from '../../utils/prisma'
import { assertChildOwnership } from '../../utils/guard'
import { z } from 'zod'

export default eventHandler(async (event) => {
  const q = getQuery(event)
  const schema = z.object({ childId: z.string() })
  const parsed = schema.safeParse(q)
  if (!parsed.success) throw createError({ statusCode: 422, statusMessage: 'INVALID_INPUT' })
  const childId = Number(parsed.data.childId)
  if (!Number.isFinite(childId)) throw createError({ statusCode: 400, statusMessage: 'BAD_ID' })
  await assertChildOwnership(event, childId)
  const items = await prisma.redemption.findMany({ where: { childId }, orderBy: { id: 'desc' } })
  return { items }
})


